package com.cqeec.bootdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.bootdemo.common.R;
import com.cqeec.bootdemo.entity.Employee;
import com.cqeec.bootdemo.service.EmployeeService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        //接受json数据格式的数据 @RequestBody
        //对用户输入的密码进行加密
        String password = DigestUtils.md5DigestAsHex(employee.getPassword().getBytes());
        //根据用户名写一个条件查询
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //        username字段     用户输入的用户名
        wrapper.eq(Employee::getUsername, employee.getUsername());
        //根据条件进行查询
        Employee emp = employeeService.getOne(wrapper);
        //登录相关的判断
        //如果为空，表示用户不存在
        if (emp == null) {
            return R.error("用户名不存在");
        }
        //查询出来的密码与用户输入的密码加密后进行对比
        if (!emp.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        //状态为0 表示账号已被禁用
        if (emp.getStatus() == 0) {
            return R.error("用户已被禁用");
        }
        request.getSession().setAttribute("employee",emp);
        return R.success(emp);
    }

    //员工管理 分页模糊查询
    //请求地址
    //请求方式 get
    @GetMapping("/page")
    public R<Page<Employee>> getPage(@RequestParam int page,
                                     @RequestParam int pageSize,
                                      String name) {
        //测试页面传来的数据
        System.out.println("page:" + page + "pageSize:" + pageSize + "name:" + name);
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        //判断名字是否为空
        wrapper.like(StringUtils.isNotBlank(name),Employee::getName, name);
        Page<Employee> page1= employeeService.page(pageInfo, wrapper);
        return R.success(page1);
    }
    //员工新增
    @PostMapping
    public R<String> add(@RequestBody Employee employee,HttpServletRequest request){
//        System.out.println("======="+employee);
        employee.setPassword(DigestUtils.md5DigestAsHex("123456" .getBytes()));
        employee.setStatus(1);
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Employee em=(Employee) request.getSession().getAttribute("employee");
        employee.setCreateUser((long)em.getId());
        employee.setUpdateUser((long)em.getId());
        employeeService.save(employee);
        return R.success("新增成功");
    }
    //修改--数据回显
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee=employeeService.getById(id);
        return R.success(employee);

    }
    //修改
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return R.success("修改成功");
    }
    //注销
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        //清空session
        return R.success("退出成功");
    }
}
